// by Xeno
private ["_anim","_mag_types","_magazines","_mags_backpack","_muzzles","_p","_primary","_s","_pistol","_base"];
_p = player;
_id = (player GetVariable "d_pbp_id");
if (_id != -9999) then {
	_p removeAction _id;
	player SetVariable ["d_pbp_id", -9999];
};
diag_log Format["(%1)[%2]: %3: %4",diag_tickTime,"backpack.sqf","Backpack start", player GetVariable "d_player_backpack"];

if (count (player GetVariable "d_player_backpack") == 0) then {
	diag_log Format["(%1)[%2]: %3: %4",diag_tickTime,"backpack.sqf","Initial content 1", player GetVariable "d_player_backpack"];
	_primary = primaryWeapon _p;
	if (_primary != "") then {
		diag_log Format["(%1)[%2]: %3: %4",diag_tickTime,"backpack.sqf","Checking pack 1", _primary];
		_mag_types = getArray(configFile>> "cfgWeapons" >> _primary >> "magazines");
		_magazines = magazines _p;
		_mags_backpack = [];
		{if (_x in _mag_types) then {_mags_backpack set [count _mags_backpack, _x]}} forEach _magazines;
		_muzzles = getArray(configFile>> "cfgWeapons" >> _primary >> "muzzles");
		if (count _muzzles == 2) then {
			_mag_types = getArray(configFile>> "cfgWeapons" >> _primary >> (_muzzles select 1) >> "magazines");
			{if (_x in _mag_types) then {_mags_backpack set [count _mags_backpack, _x]}} forEach _magazines;
		};
		_weapons = weapons _p;
		d_backpack_helper = [_magazines, _weapons];
		_pistol = "";
		{if ((getNumber (configFile >> "CfgWeapons" >> _x >> "Type")) == 2) exitWith {_pistol = _x}} forEach _weapons;
		if (_pistol != "") then {_p removeWeapon _pistol};
		{_p removeMagazine _x} forEach _mags_backpack;
		_p removeWeapon _primary;
		_anim = animationState _p;
		player SetVariable ["d_player_backpack", [_primary, _mags_backpack]];
		waitUntil {animationState player != _anim || !alive player};
		if (!alive player) exitWith {};
		diag_log Format["(%1)[%2]: %3: %4",diag_tickTime,"backpack.sqf","Done pack 1", player GetVariable "d_player_backpack"];
		_p = player;
		_s = format ["Weapon %1", [_primary,1] call XfGetDisplayName];
		if (_pistol != "") then {
			_p addWeapon _pistol; _p selectWeapon _pistol;
		};
		// if ((player GetVariable "d_pbp_id") == -9999) then {player SetVariable ["d_pbp_id", _p addAction [_s call XGreyText, "client\backpack.sqf",[],-1,false]]};
		if ((player GetVariable "d_pbp_id") != -9999) then { player removeAction (player GetVariable "d_pbp_id");};
		player SetVariable ["d_pbp_id", _p addAction [_s call XGreyText, "client\backpack.sqf",[],0,false]];
		if (alive player) then {d_backpack_helper = []};
	};
	diag_log Format["(%1)[%2]: %3: %4 (%5)",diag_tickTime,"backpack.sqf","Backpack content 1", player GetVariable "d_player_backpack",(player GetVariable "d_pbp_id")];
} else { // switch weapon
	diag_log Format["(%1)[%2]: %3: %4",diag_tickTime,"backpack.sqf","Initial content 2", player GetVariable "d_player_backpack"];
	_primary = primaryWeapon _p;
	if (_primary == "") then {
		diag_log Format["(%1)[%2]: %3: %4",diag_tickTime,"backpack.sqf","Checking pack 2", _primary];
		{_p addMagazine _x} forEach ((player GetVariable "d_player_backpack") select 1);
		_p addWeapon ((player GetVariable "d_player_backpack") select 0);
		_muzzles = getArray(configFile>>"cfgWeapons" >> ((player GetVariable "d_player_backpack") select 0) >> "muzzles");
		_p selectWeapon ((player GetVariable "d_player_backpack") select 0);
		_p selectWeapon (_muzzles select 0);
		_s = format ["%1 to Backpack", [(player GetVariable "d_player_backpack") select 0,1] call XfGetDisplayName];
		player SetVariable ["d_player_backpack", []];
		_id = (player GetVariable "d_pbp_id");
		if (_id != -9999) then {
			_p removeAction _id;
			player SetVariable ["d_pbp_id", -9999];
		};
		player SetVariable ["d_pbp_id", _p addAction [_s call XGreyText, "client\backpack.sqf",[],0,false]];
	} else {
		diag_log Format["(%1)[%2]: %3: %4",diag_tickTime,"backpack.sqf","Checking pack 3", _primary];
		_mag_types = getArray(configFile>> "cfgWeapons" >> _primary >> "magazines");
		_magazines = magazines _p;
		_mags_backpack = [];
		{if (_x in _mag_types) then {_mags_backpack set [count _mags_backpack, _x]}} forEach _magazines;
		_muzzles = getArray(configFile>> "cfgWeapons" >> _primary >> "muzzles");
		if (count _muzzles == 2) then {
			_mag_types = getArray(configFile>> "cfgWeapons" >> _primary >> (_muzzles select 1) >> "magazines");
			{if (_x in _mag_types) then {_mags_backpack set [count _mags_backpack, _x]}} forEach _magazines;
		};
		{_p removeMagazine _x} forEach _mags_backpack;
		_weapons = weapons _p;
		d_backpack_helper = [_mags_backpack, _primary];
		_pistol = "";
		{if ((getNumber (configFile >> "CfgWeapons" >> _x >> "Type")) == 2) exitWith {_pistol = _x}} forEach _weapons;
		if (_pistol != "") then {_p removeWeapon _pistol};
		_p removeWeapon _primary;
		sleep 1;
		_anim = animationState _p;
		waitUntil {animationState player != _anim || !alive player};
		if (!alive player) exitWith {};
		_p = player;
		{_p addMagazine _x} forEach ((player GetVariable "d_player_backpack") select 1);
		if (_pistol != "") then {_p addWeapon _pistol};
		_p addWeapon ((player GetVariable "d_player_backpack") select 0);
		_muzzles = getArray(configFile>> "cfgWeapons" >> ((player GetVariable "d_player_backpack") select 0) >> "muzzles");
		_p selectWeapon ((player GetVariable "d_player_backpack") select 0);
		_p selectWeapon (_muzzles select 0);
		player SetVariable ["d_player_backpack", [_primary, _mags_backpack]];
		_s = format ["Weapon %1", [_primary,1] call XfGetDisplayName];
		_id = (player GetVariable "d_pbp_id");
		if (_id != -9999) then {
			_p removeAction _id;
		};
		// if (_id == -9999) then {player SetVariable ["d_pbp_id", _p addAction [_s call XGreyText, "client\backpack.sqf",[],-1,false]]};
		player SetVariable ["d_pbp_id", _p addAction [_s call XGreyText, "client\backpack.sqf",[],0,false]];
		if (alive player) then {d_backpack_helper = []};
	};
	diag_log Format["(%1)[%2]: %3: %4 (%5)",diag_tickTime,"backpack.sqf","Backpack content 2", player GetVariable "d_player_backpack",(player GetVariable "d_pbp_id")];
};